package kamikaze.service.admin;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kamikaze.bean.User;
import kamikaze.service.UserSession;
import kamikaze.utils.Database;

import com.google.appengine.api.datastore.KeyFactory;

public class ChangeUserStatusServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		UserSession.getFrom(req).asAdmin();

		final boolean suspend = "true".equals(req.getParameter("suspend"));
		final String userId = req.getParameter("userId");

		Database.get().execute(new Database.Template<Object>() {

			@Override
			public Object query(EntityManager em) throws Exception {

				User user = em.find(User.class, KeyFactory.createKey("User", Long.valueOf(userId)));

				user.setSuspended(suspend);

				em.persist(user);

				return null;
			}
		});

		resp.sendRedirect("/admin/list_user");
	}
}
